package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ClassEntity;
import com.tfjybj.itoo.exam.provider.service.ClassService;
import com.tfjybj.itoo.exam.template.ScreenClassStu;
import com.tfjybj.itoo.teach.FacadeApi.ClassApi;
import com.tfjybj.itoo.teach.model.ClassModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * ClassController
 * class表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Api(tags = {"class表接口"})
@RequestMapping(value = "/class")
@RestController
public class ClassController {

    @Resource
    private ClassService classService;

    @Reference(check = false)
    private ClassApi classApi;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ClassModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody com.tfjybj.itoo.teach.model.ClassModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getProfessionId())) {
            return ItooResult.build(ItooResult.FAIL, "professionId为空");
        }
        com.tfjybj.itoo.teach.entity.ClassEntity classEntity = new com.tfjybj.itoo.teach.entity.ClassEntity();
        BeanUtils.copyProperties(model, classEntity);
        classApi.create(classEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        classApi.delete(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        classApi.deleteByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ClassModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id修改class")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ClassModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getProfessionId())) {
            return ItooResult.build(ItooResult.FAIL, "professionId为空");
        }
        com.tfjybj.itoo.teach.entity.ClassEntity classEntity = new com.tfjybj.itoo.teach.entity.ClassEntity();
        BeanUtils.copyProperties(model, classEntity);
        classApi.modify(classEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Class
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        com.tfjybj.itoo.teach.entity.ClassEntity classEntity = classApi.findById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classEntity);
    }

    /**
     * 分页查询所有Class
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "分页查询所有Class")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<com.tfjybj.itoo.teach.entity.ClassEntity> classs = classApi.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classs);
    }

    /**
     * 根据行政班名称模糊查询class
     *
     * @param name     行政班名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的class
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据行政班名称模糊查询class", notes = "分页根据行政班名称模糊查询class")
    @GetMapping(value = "queryClassByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryClassByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                           @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                           @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<com.tfjybj.itoo.teach.entity.ClassEntity> classList = classApi.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据professionId查找所有行政班信息
     *
     * @param professionId 专业id
     * @return classEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据professionId专业id查找所有行政班信息")
    @GetMapping(value = {"/findByProfessionId/{professionId}"})
    public ItooResult findByProfessionId(@ApiParam(value = "专业id", required = true) @PathVariable String professionId) {
        List<ClassEntity> classEntityList = classService.findByProfessionId(professionId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classEntityList);
    }

    /**
     * 根据专业id查询在校班级以及人员
     *
     * @param screenClassName  模糊查询使用的班级名称
     * @param majorId          专业id
     * @param stuNameOrStuCode 模糊查询(姓名或者学号)
     * @param startTime        考试开始时间
     * @return 班级以及班级人员信息
     * @author 马莹querySchoolClass
     * @since 2019-5-1 16:25:08
     */
    @ApiOperation(value = "根据专业id查询在校班级以及人员")
    @GetMapping(value = "/querySchoolClass/{startTime}/{endTime}")
    public ItooResult querySchoolClass(@RequestParam(required = false, defaultValue = "") String stuNameOrStuCode,
                                       @RequestParam(required = false, defaultValue = "") String screenClassName,
                                       @RequestParam(required = false, defaultValue = "") String majorId,
                                       @PathVariable String startTime, @PathVariable String endTime) {
        List<ScreenClassStu> classModelList = classService.querySchoolClass(screenClassName, majorId, startTime, stuNameOrStuCode, endTime);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功!", classModelList);
    }

}
